package com.example.gslih.myapplication.video;

import android.graphics.Color;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.MediaController;
import android.widget.VideoView;

import com.example.gslih.myapplication.R;
import com.example.gslih.myapplication.toast.TtToastUtil;


public class VideoPlayerActivity extends AppCompatActivity {
    private VideoView videoView;
    private MediaController mediaController;
    private int pointPlay = -1;

    private String url;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_player);

        if (getIntent()!=null){
            url = getIntent().getStringExtra("url");
        }

        initVideo();
    }

    private void initVideo() {
        //初始化 等待框
//        loadingAlertDialog = new LoadingAlertDialog(this);

        videoView = findViewById(R.id.video);
        videoView.setBackgroundColor(Color.BLACK);
        mediaController = new MediaController(this);

        videoView.setMediaController(mediaController);
        mediaController.setMediaPlayer(videoView);

        videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                mp.setOnInfoListener(new MediaPlayer.OnInfoListener() {
                    @Override
                    public boolean onInfo(MediaPlayer mp, int what, int extra) {
                        if (what == MediaPlayer.MEDIA_INFO_VIDEO_RENDERING_START){
                            //开始播放 关闭等待框
//                            loadingAlertDialog.dismiss();
                            videoView.setBackgroundColor(Color.TRANSPARENT);
                        }
                        return true;
                    }
                });
            }
        });

        //播放完成监听
        videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mediaPlayer) {

            }
        });

        //播放错误监听
        videoView.setOnErrorListener(new MediaPlayer.OnErrorListener() {
            @Override
            public boolean onError(MediaPlayer mediaPlayer, int i, int i1) {
                //关闭等待框
//                loadingAlertDialog.dismiss();
                TtToastUtil.showToastSetTime(VideoPlayerActivity.this,"视频资源错误",2500);
                return false;
            }
        });

        if (url != null){
            //raw中获取资源
//        Uri uri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.video);
            Uri uri = Uri.parse(url);
            videoView.setVideoURI(uri);
            videoView.requestFocus();
            //资源加载完成前 显示等待框
//            loadingAlertDialog.show();
            videoView.start();
        }else {
            TtToastUtil.showToastSetTime(this,"视频资源错误",2500);
        }
    }


    @Override
    protected void onResume() {
        super.onResume();
        if (pointPlay > 0){
            //播放资源就绪前  显示等待框
//            loadingAlertDialog.show();
            videoView.seekTo(pointPlay);
            videoView.start();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        pointPlay = videoView.getCurrentPosition();
        videoView.stopPlayback();
        videoView.setBackgroundColor(Color.BLACK);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
